package com.tensquare.notice.controller;
/*
 *ClassName:NoticeController
 *Description:
 *@author:可爱的大鸭子
 */

import com.alibaba.druid.support.spring.stat.annotation.Stat;
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.notice.pojo.Notice;
import com.tensquare.notice.pojo.NoticeFresh;
import com.tensquare.notice.service.NoticeService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("notice")
@CrossOrigin
public class NoticeController {
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private HttpServletRequest request;

    // 1. 根据id查询消息通知，需要判断是否是当前登录的用户
    // http://127.0.0.1:9016/notice/{id} GET
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public Result selectById(@PathVariable String id) {
        Notice notice = noticeService.selectById(id);
        return new Result(true, StatusCode.OK, "查询成功", notice);
    }

    // 2. 根据条件分页查询消息通知
    // http://127.0.0.1:9016/notice/search/{page}/{size} POST
    @RequestMapping(value = "search/{page}/{size}", method = RequestMethod.POST)
    public Result selectByList(@RequestBody Notice notice, @PathVariable Integer page, @PathVariable Integer size) {
        Page<Notice> pageData = noticeService.selectByPage(notice, page, size);
        PageResult<Notice> pageResult = new PageResult<>(pageData.getTotal(), pageData.getRecords());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    // 3. 新增通知
    // http://127.0.0.1:9016/notice POST
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Notice notice) {
        String id=noticeService.save(notice);
        return new Result(true, StatusCode.OK, "新增成功",id);
    }

    // 4. 修改通知，参数必须包含要修改的通知的id
    // http://127.0.0.1:9014/notice PUT
    @RequestMapping(method = RequestMethod.PUT)
    public Result updateById(@RequestBody Notice notice) {
        noticeService.updateById(notice);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    // 5. 根据用户id查询该用户的待推送消息（新消息）
    // http://127.0.0.1:9016/notice/fresh/{userId}/{page}/{size} GET
    @RequestMapping(value = "fresh/{userId}/{page}/{size}", method = RequestMethod.GET)
    public Result freshPage(@PathVariable String userId, @PathVariable Integer page, @PathVariable Integer size) {
        Page<NoticeFresh> pageData = noticeService.freshPage(userId,page,size);
        PageResult<NoticeFresh> pageResult = new PageResult<>(pageData.getTotal(), pageData.getRecords());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    // 6. 删除待推送消息（新消息）
    // http://127.0.0.1:9016/notice/fresh DELETE
    @RequestMapping(value = "fresh",method = RequestMethod.DELETE)
    public Result freshDelete(@RequestBody NoticeFresh noticeFresh) {
        noticeService.freshDelete(noticeFresh);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    //删除消息通知
    @RequestMapping(method = RequestMethod.DELETE)
    public Result delete(@RequestBody Notice notice) {
        noticeService.delete(notice);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    //查询当前登录的用户所有未读的消息列表
    @RequestMapping(value = "/allNotices",method = RequestMethod.GET)
    public Result findAllNotices(){
        Claims claims=(Claims)request.getAttribute("user_claims");
        if(claims==null) {
            System.out.println("请先登录！");
            return new Result(false,StatusCode.ACCESSERROR,"请先登录");
        }
        String userId = claims.getId();
        System.out.println("id："+userId);
        List<Notice> list=noticeService.findAllNotices(userId);
        return new Result(true, StatusCode.OK,"查询成功",list);
    }

    //设置某条消息已读
    @RequestMapping(value = "/readed/{nid}",method = RequestMethod.PUT)
    public Result setReaded(@PathVariable String nid){
        Claims claims=(Claims)request.getAttribute("user_claims");
        if(claims==null) {
            System.out.println("请先登录！");
            return new Result(false,StatusCode.ACCESSERROR,"请先登录");
        }
        String userId = claims.getId();
        noticeService.setReaded(nid);
        return new Result(true, StatusCode.OK,"已读");
    }

    //设置未读消息全部已读
    @RequestMapping(value = "allReaded",method = RequestMethod.PUT)
    public Result setAllReaded(){
        Claims claims=(Claims)request.getAttribute("user_claims");
        if(claims==null) {
            System.out.println("请先登录！");
            return new Result(false,StatusCode.ACCESSERROR,"请先登录");
        }
        String userId = claims.getId();
        noticeService.setAllReaded(userId);
        return new Result(true, StatusCode.OK,"全部已读");
    }

    //获取我的未读消息的数量
    @RequestMapping(value = "myMessNum",method = RequestMethod.GET)
    public Result getMyMessNum(){
        Claims claims=(Claims)request.getAttribute("user_claims");
        if(claims==null) {
            System.out.println("请先登录！");
            return new Result(false,StatusCode.ACCESSERROR,"请先登录");
        }
        String userId = claims.getId();
        int num=noticeService.getMyMessNum(userId);
        return new Result(true, StatusCode.OK,"查询成功",num);
    }
}
